package com.it.mybatis.spring;

import com.it.mybatis.spring.sqlSessionFactory.Configuration;
import com.it.mybatis.spring.sqlSessionFactory.DefaultSqlSessionFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 胡凯
 * @create 2021/11/25 19:01
 * 通过sqlBuilder 创建sqlsessionfactroy,在通过sqlsessionfactroy创建sqlSession
 */
public class SqlSessionFactoryBuilder {

    /* public DefaultSqlSessionFactory build(Reader reader) throws DocumentException {
         // TODO: 2023/3/23
         DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory();
        Document document =  new SAXReader().read(reader);



    } */
    public Configuration parseConfig(Element root){
        Configuration configuration = new Configuration();
        List<Element> elementList =  root.selectNodes("//dataSource");
        configuration.setDataSource(dataSource(elementList));

        return configuration;
    }
    public Map<String ,String> dataSource(List<Element> elementList ){
        Map<String, String> dataSource = new HashMap<>(4);
        Element element = elementList.get(0);
        List content = element.content();
        for (Object o : content) {
            Element e = (Element) o;
            String name = e.attributeValue("name");
            String value = e.attributeValue("value");
            dataSource.put(name, value);
        }
        return dataSource;
    }
}
